clear
set more off
program drop _all

use "INTERMEDIATE/product_RMS_def10_firm_SampleE_06_15_annual.dta", clear
drop if firm==. | group==.  
egen firmgroup=group(firm group)
drop if age==0     
gen entry=1 if age==1
gen exit=1 if age==maxAge
drop if type=="exit"

bys firmgroup year: egen N_ijt = sum(entry)
bys firmgroup year: egen X_ijt = sum(exit)
gen all=1
bys firmgroup year: egen T_ijt = sum(all)

* Total revenue within the firmgroup:
bys firmgroup year: egen rev_ijt = sum(revenue)
bys firmgroup year: egen rev_r_ijt = sum(revenue_r)
bys firmgroup year: egen rev_mean_ijt = mean(revenue)
bys firmgroup year: egen rev_r_mean_ijt = mean(revenue_r)

* Total revenue within the firmgroup:
bys firmgroup year: egen revN_ijt = sum(revenue*entry)
bys firmgroup year: egen revN_r_ijt = sum(revenue_r*entry)
bys firmgroup year: egen revN_mean_ijt = mean(revenue*entry)
bys firmgroup year: egen revN_r_mean_ijt = mean(revenue_r*entry)

* Total revenue within the firmgroup:
bys firmgroup year: egen revX_ijt = sum(revenue*exit)

xtset product year

duplicates drop firmgroup firm group year *_ijt, force
keep firmgroup firm group year  *_ijt
order  firmgroup firm group year  *_ijt

// Defines cohort/entryTime
bysort firm group: egen xx=min(year)
qui su xx
gen lcensored_ij=(xx==`r(min)') 
label variable lcensored_ij "Firm already exists in 2006"
gen cohort_ij=xx 
label variable cohort_ij "First year we observe the firm, 2006 is censored"

// Defines ExitTime
bysort firm group: egen yy=max(year)
qui su yy
gen rcensored_ij=(yy==`r(max)') 
label variable rcensored_ij "Firm continues beyond 2015"
gen exitTime_ij=yy -1                                     
label variable exitTime_ij "Last year we observed the firm, 2015 is censored"

// Defines Age
gen age_ijt=year-cohort_ij
replace age_ijt=. if year>exitTime_ij
label variable age_ijt "Age of the firm, for lcensored means time in dataset"
bysort firm group: egen maxAge_ij=max(age_ijt) 

// Type of observation 
gen type_ijt="NA"
replace type_ijt="entry"  if age==0 & age!=maxAge  
replace type_ijt="continues"  if age>0  & age<maxAge  
replace type_ijt="exit"       if age==maxAge & age!=0 
replace type_ijt="entry/exit" if age==maxAge & age==0 
gen gamma_ijt=1      if type=="entry"
replace gamma_ijt=0  if type=="continues"
replace gamma_ijt=-1 if type=="exit"
gen obs=1 if T_ijt>0
bysort firmgroup: egen totalobs_ij=sum(obs)
replace totalobs_ij= totalobs_ij -1
gen flag_longitudinal_ij="NA"
replace flag_longitudinal_ij="Complete" if totalobs_ij==yy-xx
replace flag_longitudinal_ij="Incomplete" if totalobs_ij<yy-xx
drop xx yy obs 

* deal with the first 2 quarters and the last two quarter

// 2006 
su year
foreach var of varlist N*_ijt revN*  {
replace `var'=. if year==`r(min)'
} 
 
*********************************************************************
*************************   DEFINE VARIABLES   **********************
*********************************************************************

// Sample
drop firmgroup
egen firmgroup=group(firm group)
xtset firmgroup year 
keep if flag_longitudinal_ij=="Complete"

// levels
gen Y  = rev_ijt 
gen T  = T_ijt      
gen y  = Y/T

gen YN = revN_ijt  
gen N  = N_ijt      
gen yN = YN/N  

gen YC = Y-YN   
gen C=T-N
gen yC=YC/C
gen LyC=(L.Y-L.revX_ijt)


// Changes 1
gen C_Y    = (Y-L.Y)/L.Y   
gen C_YC   = (YC-L.Y)/L.Y
gen C_yC   = (YC-LyC)/L.Y
gen C_C    =  C_YC- C_yC
gen C_YN   = YN/L.Y
gen C_N    = N/L.T
gen C_yN   = yN/L.y  


// Changes 2
gen C2_Y   = (Y-L.Y)/(0.5*Y+0.5*L.Y)   
gen C2_YC  = (YC-L.Y)/(0.5*Y+0.5*L.Y) 
gen C2_yC  = (YC-LyC)/(0.5*Y+0.5*L.Y) 
gen C2_C   = C2_YC-C2_yC
gen C2_YN  = YN/(0.5*Y+0.5*L.Y) 
gen C2_N   = N/(0.5*T+0.5*L.T)   
gen C2_yN  = yN/(0.5*y+0.5*L.y)  


// Weights
xtset firmgroup year
gen weight=L.Y   
gen weight2=0.5*Y+0.5*L.Y    
bys firm group: egen weight1=mean(Y)
bys firm group: egen weight12=median(Y)
bys group: egen weight3=sum(Y)

drop if year==2016 |  year==2015  
drop if year==2006 | year==2007 
keep if year!=exitTime_ij

foreach var of varlist C_* {
winsor2 `var',  suffix(`var'_tr0) trim cuts(2.5 97.5)
winsor2 `var', suffix(`var'_w) cuts(2.5 97.5)
}

foreach var of varlist C_* C2_* {
winsor2 `var', trim cuts(2.5 97.5)
winsor2 `var', cuts(2.5 97.5)
}
drop *_tr0_tr *_w_tr
gen temp1=0 if  C_Y_tr ==. | C_YC_tr==. | C_YN_tr==. | C_yN_tr==.
gen temp2=0 if  C2_Y_tr ==. | C2_YC_tr==. | C2_YN_tr==. | C2_yN_tr==.

foreach var of varlist C_*tr {
replace `var'=. if temp1==0
}
foreach var of varlist C2_*tr {
replace `var'=. if temp2==0
}


*********************************************************************
**************** TABLES WITH AVERAGE BY AGE  ************************
*********************************************************************

// New variables
gen table_age=" "
replace table_age="1-2" if (age_ijt==1 & lcensored_i==0)| (age_ijt==2 & lcensored_i==0)
replace table_age="3-4" if (age_ijt==3 & lcensored_i==0) | (age_ijt==4 & lcensored_i==0)
replace table_age="5-7" if (age_ijt==5 & lcensored_i==0) | (age_ijt==6 & lcensored_i==0)| (age_ijt==7 & lcensored_i==0)
replace table_age="Born before 2006" if lcensored_i==1

gen table_age2=" "
replace table_age2="1" if (age_ijt==1 & lcensored_i==0)
replace table_age2="2" if (age_ijt==2 & lcensored_i==0)  
replace table_age2="3-4" if (age_ijt==3 & lcensored_i==0) | (age_ijt==4 & lcensored_i==0)
replace table_age2="Born before 2006" if lcensored_i==1

gen table_age3=" "
replace table_age3="1" if (age_ijt==1) & lcensored_i==0
replace table_age3="2-4" if (age_ijt>=2 & age_ijt<=5  & lcensored_i==0 )
replace table_age3="Born before 2006" if lcensored_i==1
 
// Tables main Variables
global list1  C_Y C_YC C_yC C_C C_YN C_N C_yN
global list2  C_Y_tr C_YC_tr C_yC_tr C_C_tr C_YN_tr C_N_tr C_yN_tr 
global list3  C_Y_w C_YC_w C_yC_w C_C_w C_YN_w C_N_w C_yN_w 
label variable C_Y "Growth Sales"
label variable C_YC "Product Life Cycle Component"
label variable C_yC " \quad Growth of Surviving"
label variable C_C  " \quad Sales Share of Exit"
label variable C_YN "New Products Component"
label variable C_N  " \quad Entry Rate"
label variable C_yN " \quad Entrants Relative Sales"

label variable C_Y_tr "Growth Sales"
label variable C_YC_tr "Product Life Cycle Component"
label variable C_yC_tr " \quad Growth of Surviving"
label variable C_C_tr  " \quad Sales Share of Exit"
label variable C_YN_tr "New Products Component"
label variable C_N_tr " \quad Entry Rate"
label variable C_yN_tr " \quad Entrants Relative Sales"

label variable C_Y_w "Growth Sales"
label variable C_YC_w "Product Life Cycle Component"
label variable C_yC_w " \quad Growth of Surviving"
label variable C_C_w  " \quad Sales Share of Exit"
label variable C_YN_w "New Products Component"
label variable C_N_w " \quad Entry Rate"
label variable C_yN_w " \quad Entrants Relative Sales"

su $list1 if table_age!=" "
eststo age1: estpost summarize $list1 if table_age=="1-2"
eststo age2: estpost summarize $list1 if table_age=="3-4"
eststo age3: estpost summarize $list1 if table_age=="5-7"
eststo age4: estpost summarize $list1 if table_age=="Born before 2006"
eststo age: estpost summarize $list1 if table_age!=" "
esttab age age1 age2 age3 age4 using "OUTPUT/Table3.tex", cells("mean(pattern(1 1 1) fmt(3))")  mtitles("All" "Ages 1-2" "Ages 3-4"  "Ages 5-7"  "Born before 2006") collabels(none) noobs replace label
eststo age1: estpost summarize $list1 if table_age=="1-2" [w=weight]
eststo age2: estpost summarize $list1 if table_age=="3-4" [w=weight]
eststo age3: estpost summarize $list1 if table_age=="5-7" [w=weight]
eststo age4: estpost summarize $list1 if table_age=="Born before 2006" [w=weight]
eststo age: estpost summarize $list1 if table_age!=" " [w=weight]

esttab age age1 age2 age3 age4 using "OUTPUT/Table3.tex", cells("mean(pattern(1 1 1) fmt(3))")  mtitles("All" "Ages 1-2" "Ages 3-4"  "Ages 5-7"  "Born before 2006") collabels(none) noobs replace label

